<%String nav="result"; %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="com.google.appengine.api.users.*" %>
<%
  UserService userService = UserServiceFactory.getUserService();
  User user = userService.getCurrentUser();
  String authURL = (user != null) ? userService.createLogoutURL(request.getRequestURI()): userService.createLoginURL(request.getRequestURI());
%><!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Show Minifier - Visual web page compressor</title>
    <link type="text/css" rel="stylesheet" href="/css/project.css" />
  </head>
  <body>
  <div class="page">
  <div class="head">
    <div class="signin">
      <% if (user != null) { %>
         <%= user.getEmail() %> | <a href="/settings">settings</a> | 
      <% } %>
      <a href="<%= authURL %>">
        <% if (user != null) { %>Log out<% } else  { %>Log in<% } %>
      </a>
    </div>
    <h1>
        Show Minifier
    </h1>
    <ul class="nav">
    	<li><a <% if(nav.equals("home")){%>class="selected" <%}%>href="/">Home</a>
        <li><a <% if(nav.equals("test")){%>class="selected" <%}%>href="/test">New Test</a>
        <li><a <% if(nav.equals("result")){%>class="selected" <%}%>href="/result">Test Result</a>
        <li><a href="http://code.google.com/p/showminifier">Download</a>
    	<!--li><a <% if(nav.equals("performance")){%>class="selected"<%}%> href="/performance">Performance</a-->
    	<!--li><a <% if(nav.equals("help")){%>class="selected"<%}%> href="/help">Help</a-->
        <li><a href="http://groups.google.com/group/showminifier">Forums</a>
    </ul>
    </div>
<div class="body">
	<div class="resultList">
<%@ page import="java.text.*"%>
<%@ page import="org.showminifier.web.*" %>
<%@ page import="java.util.*" %>

<%
Long keyOffsetId = null;
String keyOffset = request.getParameter("page");
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
DecimalFormat df = new DecimalFormat("###.#");
String queryDate = request.getParameter("date");
String lable = request.getParameter("lable");
User reqUser = null;
String owner = request.getParameter("owner");
long oneDay = 1000 * 60 * 60 * 24;
if (keyOffset != null) keyOffsetId = Long.decode(keyOffset);
Date offsetDate = null;

if(owner != null && owner.equals("true") && user != null){
	reqUser = user;
}
if(queryDate != null) 
try{
	offsetDate = sdf.parse(queryDate);
}catch(ParseException e){};

ArrayList<WebContent> entries = new ArrayList<WebContent>(WebContentUtil.getContents(keyOffsetId,offsetDate,lable,reqUser));
if(entries == null){%>
	<div class="error">Some error occured while connecting database.Please wait for a moment.</div>
<% 
}else{
WebContent lastEntry = null;
if (entries.size() > WebContentUtil.ENTITIES_PER_PAGE) {
  lastEntry = entries.get(WebContentUtil.ENTITIES_PER_PAGE);
  entries.remove(WebContentUtil.ENTITIES_PER_PAGE);
}
if (!entries.isEmpty()) {
%>

	<table>
	<caption>Recent tests
	<thead><tr><th>Timestamp<th>Original size<th>Minified size<th>Savings<th>Lable<th>Performance<th>URL
	<%for (WebContent entry : entries) { 
		int originalLength = entry.getPage().getOriginalContent().getValue().length();
		int minifiedLength = entry.getPage().getMinifiedContent().getValue().length();
		int originalDeflatedLength = entry.getPage().getOriginalDeflatedLength();
		int minifiedDeflatedLength = entry.getPage().getMinifierDefaltedLength();
		int length = (entry.getPage().isDeflated() ? originalDeflatedLength : originalLength);
		String url = entry.getUrl().getValue();
		url = url.length() > 50 ? url.substring(0,50) + "..." : url; 
	%>
	<tr><td><%= sdf.format(entry.getRequestDate()) %>
	<td><%= df.format(originalLength / 1024D) + "KB (" + df.format(originalDeflatedLength / 1024D) + "KB gzipped)" %>
	<td><%= df.format(minifiedLength / 1024D) + "KB (" + df.format(minifiedDeflatedLength / 1024D) + "KB gzipped)" %>
	<td><%= //df.format((originalLength - minifiedLength) / 1024D) + "KB ( "  + df.format((length - minifiedDeflatedLength) / 1024D) + "KB gzipped )"
		df.format((originalLength - minifiedLength) / 1024D) + "KB ( "  + df.format((originalDeflatedLength - minifiedDeflatedLength) / 1024D)  + "KB gzipped )"%>
	<td><a href="/?lable=<%= entry.getLable() %>"><%= entry.getLable() %></a>
	<td><a href="/performance/detail?key=<%= entry.getId() %>">View performance</a>
	<td><a href="/display?key=<%= entry.getId() %>"><%= url %>
	<%} %>
	</table>
	 <div>
	<%if (user != null){%> <a href="/?owner=true">View my minified page</a> <% } %> 
	 <%if (user != null && lastEntry != null){%> | <%} %>
	<%if (lastEntry != null) {%> <a href="/?page=<%= lastEntry.getId() %>">Next page</a>  <%} %>
	</div>
<%}
}%> 
</div>
</div>
<%@ include file="include/foot.jsp"%>
</div>
<script src="/js/display.js"></script>
